package com.example.project.dao;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.example.project.XMLParser;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteDireccion extends SQLiteOpenHelper{
	

	static final String KEY_RESTAURANT = "restaurant"; 
 	static final String KEY_ID = "idDireccion";
 	static final String KEY_VIA = "via";
 	static final String KEY_NUMERO = "numero";
 	static final String KEY_CP = "cp";
 	static final String KEY_PLANTA = "planta";
 	static final String KEY_ESCALERA="escalera";
 	static final String KEY_PUERTA="puerta";
 	static final String KEY_LONGITUD="longitud";
 	static final String KEY_LATITUD="latitud";
 	static final String URL = "https://sites.google.com/site/balaguerrcardsp/menjar/menjar.xml";
	static final String TABLE_NAME = "direccion";
	
    public SQLiteDireccion(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	
    String sqlCreate = "CREATE TABLE direccion ( idDireccion integer primary key autoincrement, via TEXT, numero integer, CP TEXT, planta integer, escalera integer, puerta integer, longitud TEXT, latitud TEXT)";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sqlCreate);
        this.init(db);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
        db.execSQL("DROP TABLE IF EXISTS comer");
        db.execSQL(sqlCreate);
    }
    
    private void init(SQLiteDatabase db){
    	XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); 
		Document doc = parser.getDomElement(xml); 
		NodeList nl = doc.getElementsByTagName(KEY_RESTAURANT);
		ContentValues values = new ContentValues(); 
    	
		for (int i = 0; i < nl.getLength(); i++) {
		
			db.insert(SQLiteComer.TABLE_NAME, null, values);
		}
		
		
    	
    }

}
